package com.building18.controller;

import com.building18.handler.MyAssert;
import com.building18.pojo.Log;
import com.building18.pojo.MyPageData;
import com.building18.pojo.Result;
import com.building18.pojo.enums.Code;
import com.building18.service.LogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.coyote.Request;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/log")
@Tag( name = "日志管理", description = "日志管理接口")
public class LogController {
  @Autowired
    private LogService logService;

    @GetMapping("/{pageNum}/{pageSize}")
    @Operation(summary = "分页查询")
    public Result<?> selectLog(@PathVariable("pageNum") Integer pageNum,@PathVariable("pageSize") Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Log> logList=logService.selectLog();
        PageInfo<Log> pageInfo=new PageInfo<>(logList);
        MyPageData<Log> myPageData=new MyPageData<>(pageInfo.getTotal(),pageInfo.getList());
        return Result.ok(myPageData, Code.SELECT_SUCCESS.getMessage());
  }

  @DeleteMapping("/deleteLog")
    @Operation(summary = "根据id删除")
  public Result<?> deleteLog(@RequestBody Integer ids){
        Integer i=logService.deleteLog(ids);
      MyAssert.error(i<=0,Code.DELETE_FAIL.getMessage());
      return Result.ok(Code.DELETE_SUCCESS.getMessage());
  }

  @DeleteMapping ("/deleteLogs")
    @Operation(summary="批量删除")
    public Result<?> deleteLogs(@RequestBody List<Integer> ids){
        Integer i=logService.deleteLogs(ids);
        MyAssert.error(i<=0,Code.DELETE_FAIL.getMessage());
        return Result.ok(Code.DELETE_SUCCESS.getMessage());
  }
}
